/*
   Kinect - Qt Kinect Wrapper Class
   Copyright (C) 2012-2112:
         Javier Ignacio Cuneo, javier.cuneo@hospitalitaliano.org.ar

   All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
    Se lo dedico a Guido para vos que decias que no se podia.
*/
#ifndef KINECT_H
#define KINECT_H

#include <QWidget>
#include <QPainter>
#include <QThread>
#include <QMutex>
#include <XnOpenNI.h>
#include <XnCppWrapper.h>

class Kinect: public QThread
{
    Q_OBJECT
public:
    enum KinectStatus
    {
       Idle = 0,
       Initializing = 1,
       OkRun = 2,
       ErrorStop = 3
    };
    enum CalibrationStatus
    {
       CalibrationStart = 0,
       CalibrationEndSuccess = 1,
       CalibrationEndFail = 2
    };
    Kinect();
    virtual ~Kinect();
   void run();
   void start();
   void stop();
   bool isRunning();
   bool isStopped();
   void setDisplayInfoDepth(bool draw);
   void setDisplayInfoImage(bool draw);
   unsigned getFrameID();
   double getTimestamp();
   void getCameraDepth(QImage &camera,QImage &depth,double &ts,unsigned &fid);
   QImage getDepth();
   QImage getCamera();
   QString getErrorMsg();

private:
   // <-- Data to export outside of the thread
   unsigned frameid;
   QImage imageDepth,imageCamera;
   double timestamp;
   QString errorMsg;
   // --> End data to export outside of the thread
   bool t_requeststop;
   QMutex mutex;
   volatile KinectStatus status;                         // 0: pre-init. 1: init. 2: ok-run. 3:error-stop
   xn::Context g_Context;
   xn::DepthGenerator g_DepthGenerator;
   xn::ImageGenerator g_ImageGenerator;
   xn::Recorder g_recorder;
   xn::Recorder g_recorder2;
   XnChar g_strPose[20];
   bool displayInfoDepth;
   bool displayInfoImage;
   QImage createDepthImage();
   QImage createCameraImage();
   //bool initializexml();
   bool initialize();

   void getJointProj(XnSkeletonJointPosition joint,XnPoint3D &proj,bool &valid);

   void drawInfo(QPainter *painter);

private slots:

signals:
   void dataNotification();
   void statusNotification(Kinect::KinectStatus);
   void calibrationNotification(unsigned,Kinect::CalibrationStatus);

};
// Register types used in signal/slot mechanism
Q_DECLARE_METATYPE(Kinect::KinectStatus);
Q_DECLARE_METATYPE(Kinect::CalibrationStatus);
#endif // KINECT_H



